﻿using System;
using System.Collections.Generic;

using System.Text;
using Portal.Interface;
using Portal.Environment;
using Portal.Language;
using System.Web.UI.WebControls;
using System.Web;

namespace Portal.CMS
{
    public class UploadDocument : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "UploadDocument";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Upload tài liệu mới");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Description", "");
            }
        }


        System.Web.UI.WebControls.Button SaveButtonLargeOnly = null;
        System.Web.UI.WebControls.Label labelError = null;
        System.Web.UI.WebControls.TextBox txtDocumentItemName = null;
        System.Web.UI.WebControls.TextBox txtDescription = null;
        System.Web.UI.WebControls.TextBox txtUrl = null;

        public override void LoadSubControls(System.Web.UI.WebControls.PlaceHolder pl, System.Web.UI.Page Page, LoadParamCls LoadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UploadDocument));

            labelError = new System.Web.UI.WebControls.Label();
            labelError.CssClass = "ErrorUploadNewsImage";

            SaveButtonLargeOnly = new Button();
            SaveButtonLargeOnly.Text = ResourceBLL.GetText(WebPartId, "Upload", "Upload tài liệu");
            SaveButtonLargeOnly.Click += new EventHandler(SaveButtonLargeOnly_Click);


            txtDescription = new TextBox();
            txtDescription.TextMode = System.Web.UI.WebControls.TextBoxMode.MultiLine;

            txtUrl = new TextBox();
            txtUrl.CssClass = "cssUploadAdvImageItem";

            txtDocumentItemName = new TextBox();
            txtDocumentItemName.CssClass = "cssUploadAdvImageItem";


            pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetHeaderFormDialog(ResourceBLL.GetText(WebPartId, "Title", "Thêm mới tài liệu vào album!"))));
            pl.Controls.Add(labelError);
            pl.Controls.Add(new System.Web.UI.LiteralControl("<table>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl(ResourceBLL.GetText(WebPartId, "FileName", "Upload file:")));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<input name=\"txtItemName\" id=\"txtItemName\" type=\"file\" runat=server>"));
            pl.Controls.Add(SaveButtonLargeOnly);
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));


            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>" + ResourceBLL.GetText(WebPartId, "Subject", "Tiêu đề") + ":</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(txtDocumentItemName);
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));


            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>" + ResourceBLL.GetText(WebPartId, "Url", "Url") + ":</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(txtUrl);
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));

            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>" + ResourceBLL.GetText(WebPartId, "Description", "Mô tả") + ":</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(txtDescription);
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));



            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td></td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));

            pl.Controls.Add(new System.Web.UI.LiteralControl("<input type=button onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "CloseButton", "Thoát") + "\">"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</table>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetFooterFormDialog()));
        }

        void SaveButtonLargeOnly_Click(object sender, EventArgs e)
        {
            try
            {
                string DocumentId = (string)System.Web.HttpContext.Current.Request["DocumentId"];
                DocumentCls DocumentCls = new DocumentBLL().CreateModel(Environments.ActiveLanguage, DocumentId);
                labelError.Text = "";
                HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;

                HttpPostedFile hf = (HttpPostedFile)files[0];

                string PathSave = Environments.WorkingPath + "\\media\\" + Environments.PortalAlias;
                System.IO.Directory.CreateDirectory(PathSave);

                PathSave = Environments.WorkingPath + "\\media\\" + Environments.PortalAlias + "\\Document";
                System.IO.Directory.CreateDirectory(PathSave);

                PathSave = Environments.WorkingPath + "\\media\\" + Environments.PortalAlias + "\\Document\\" + DocumentCls.belongSite;
                System.IO.Directory.CreateDirectory(PathSave);

                PathSave = Environments.WorkingPath + "\\media\\" + Environments.PortalAlias + "\\Document\\" + DocumentCls.belongSite + "\\Archives";
                System.IO.Directory.CreateDirectory(PathSave);

                string FileSave = PathSave + "\\" + hf.FileName;
                string UploadName = "";
                byte[] b = null;
                if (hf.FileName.Length > 0)
                {
                    hf.SaveAs(FileSave);
                    UploadName = new System.IO.FileInfo(FileSave).Name;
                    b = Environments.GetBytesFromFile(FileSave);
                }

                ServerSideUploadLarge(DocumentCls.belongSite, DocumentId, FileSave,b);
                labelError.Text = ResourceBLL.GetText(WebPartId, "UploadOk", "Thêm mới tài liệu vào thành công!");
                txtDescription.Text = "";
                txtDocumentItemName.Text = "";
                txtUrl.Text = "";
                try
                {
                    System.IO.File.Delete(FileSave);
                }
                catch
                {
                }
            }
            catch (Exception ex)
            {
                labelError.Text = ex.Message.ToString();
            }
        }



        [AjaxPro.AjaxMethod]
        public string ServerSideUploadLarge(string belongSite, string DocumentId, string UploadFile,byte[] b)
        {
            new DocumentBLL().AddDocumentItem(null, belongSite, DocumentId, UploadFile, txtDocumentItemName.Text, txtUrl.Text, txtDescription.Text, b);
            return ResourceBLL.GetText(WebPartId, "MessageUploadOk", "Thêm mới và upload thành công!");
        }
    }
}